<?
class UnableToLoadClassException extends Exception{}

class ss_core_file {
	static public function autoload($class){
		//ss_error::log($class,"Autoloading");
		$segments=explode('_',$class);
		$filenames=array();
		$filenames[]=implode(DIRECTORY_SEPARATOR,array_slice($segments,0,count($segments)-1)).DIRECTORY_SEPARATOR.$class.'.php';
		$filenames[]=implode(DIRECTORY_SEPARATOR,array_slice($segments,0,count($segments)-1)).DIRECTORY_SEPARATOR.
		implode('_',array_slice($segments,0,count($segments)-1)).'.php';
		$filenames[]=implode(DIRECTORY_SEPARATOR,array_slice($segments,0,count($segments)-2)).DIRECTORY_SEPARATOR.
		implode('_',array_slice($segments,0,count($segments)-1)).'.php';
		$filenames[]=implode(DIRECTORY_SEPARATOR,$segments).'.php';
		foreach($filenames as $filename) {
			//ss_error::log($filename,"Trying file for {$class}");
			if ($_file=self::searchIncludePath($filename)) {
				//ss_error::log($_file,"Loading {$class} from");
				return require_once($_file);
			}
		}
		return false;
		//throw new UnableToLoadClassException($class);
	}
	
	static public function searchIncludePath($filename){
		if (is_file($filename)) return $filename;
		foreach (explode(PATH_SEPARATOR, ini_get("include_path")) as $path) {
			if (strlen($path) > 0 && $path{strlen($path)-1} != DIRECTORY_SEPARATOR) $path .= DIRECTORY_SEPARATOR;
			$f = realpath($path . $filename);
			if ($f && is_file($f)) return $f;
		}
		return false;
	}
}

?>